## package students

window.students = {}

## private

$grid = null
$data_view = null
$name_pattern = '\\l{6}\\f{1}'
$email_suffix = 'you.school.nz'

ClassesCellFormatter = (row, cell, value, columnDef, dataContext) ->
  text = ""
  for language in value
    text += "#{language.la} #{language.cl}, "
  text.slice -2

setup_grid = (students) ->
  columns = [
    {
      id:"un", title:"Username", sortable: true
    },
    {
      id:"rn", title:"Real Name", sortable: true
    },
    {
      id:"cl", title:"Classes", sortable: false,
      formatter:ClassesCellFormatter
    }
  ]
  $grid = $('#students-grid').powergrid columns, students

setup_settings = (name_pattern, email_suffix) ->
  if name_pattern is ""
    $('#set-s-username-pattern').hide()
  if email_suffix is ""
    $('#set-s-email-suffix').hide()
  return unless name_pattern and email_suffix
  $name_pattern = name_pattern
  $email_suffix = email_suffix
  generate_name_example $name_pattern
  generate_email_example $email_suffix
  #TODO: Much better system of doing usernames
  if (match = name_pattern.match /^\\(f|l)\{([0-9]+)\}/)
    console.log "Matched"
    if match[1] is 'f'
      $('#s-pattern-s1 option').removeAttr 'selected'
      $('#s-pattern-s1 option').first().attr 'selected', true
    $('#s-pattern-n1').val match[2]
  if (match = name_pattern.match /\\(f|l)\{([0-9]+)\}$/)
    if match[1] is 'l'
      $('#s-pattern-s2 option').removeAttr 'selected'
      $('#s-pattern-s2 option').last().attr 'selected', true
    $('#s-pattern-n2').val match[2]

refresh_name_pattern = ->
  $name_pattern = ""
  for i in [1..2]
    if $("#s-pattern-s#{i}").val() is "First Name"
      $name_pattern += '\\f'
    else
      $name_pattern += '\\l'
    if (val = parseInt($("#s-pattern-n#{i}").val())) > 0
      $name_pattern += "{#{val}}"
    else
      $name_pattern += "{0}"
  generate_name_example $name_pattern

generate_name_example = (pattern) ->
  console.log pattern
  firstname = "timothy"
  lastname = "hampton"
  pattern.match /\\f\{([0-9]+)\}/
  pattern = pattern.replace /\\f\{([0-9]+)\}/, firstname.substring(0, RegExp.$1)
  pattern.match /\\l\{([0-9]+)\}/
  pattern = pattern.replace /\\l\{([0-9]+)\}/, lastname.substring(0, RegExp.$1)
  $('#s-username-example').text app.school_id + '.' + pattern
  generate_email_example $email_suffix

generate_email_example = (suffix) ->
  username = $('#s-username-example').text()
  $('#s-email-example').text username.substr(4) + '@' + suffix

setup_individual_settings = ->
  [firstname, lastname] = $('#i-s-name').val().toLowerCase().split(' ')
  pattern = $name_pattern
  pattern.match /\\f\{([0-9]+)\}/
  pattern = pattern.replace /\\f\{([0-9]+)\}/, firstname.substring(0, RegExp.$1)
  pattern.match /\\l\{([0-9]+)\}/
  pattern = pattern.replace /\\l\{([0-9]+)\}/, lastname.substring(0, RegExp.$1)
  $('#s-i-pattern-username').text app.school_id + '.' + pattern
  $('#s-i-pattern-email').text pattern + '@' + $email_suffix

confirm_individual_student = ->
  $('#confirm-student-i').button 'loading'

  args = { i: app.school_id, tk: app.token }
  if $('#s-i-pattern-username').is(':visible')
    args.un = $('#s-i-pattern-username').text()
  else
    args.un = $('#s-i-email-input').val()
  args.rn = $('#i-s-name').val()

  $.post '/school/student/save', args, (data) ->
    if data.error
      $.meow { message: "The server doesn't like your values ... Check the form is valid, and if it is then <a target='_blank' href='/contact'>contact us!</a>" }
    else
      $('#add-student-i').modal 'hide'
    $('#confirm-student-i').button 'reset'

## public

students.onSwitch = ->
  console.log $grid
  $grid.powerlayout()

## initialize

students.initialize = (data) ->
  console.log data
  setup_grid data.st
  setup_settings data.sp, data.se
  $('#student-settings-btn').click ->
    $('#student-settings').modal 'show'
  $('#snp-choice a').click ->
    if this.innerHTML.indexOf("Pattern") isnt -1
      $('#set-s-username-pattern').show()
      refresh_name_pattern()
    else
      $('#set-s-username-pattern').hide()
      $name_pattern = null
  $('#ses-choice a').click ->
    if this.innerHTML.indexOf("Pattern") isnt -1
      $('#set-s-email-suffix').show()
      $email_suffix = $('#student-email-suffix').val()
      generate_email_example $email_suffix
    else
      $('#set-s-email-suffix').hide()
      $email_suffix = null
  for i in [1..2]
    $("#s-pattern-s#{i}").change refresh_name_pattern
    $("#s-pattern-n#{i}").change refresh_name_pattern
  $('#student-email-suffix').change ->
    $email_suffix = $(this).val()
    generate_email_example $email_suffix
  $('#student-settings-ok').click ->
    $('#student-settings-ok').button 'loading'
    $.post '/school/settings/student/save', { i: app.school_id, tk: app.token, p: ($name_pattern or ""), e: ($email_suffix or "") }, (data) ->
      if data.error
        $.meow { message: "The server doesn't like your values ... Check the form is valid, and if it is then <a target='_blank' href='/contact'>contact us!</a>" }
      else
        $('#student-settings').modal 'hide'
      $('#ok-student-settings').button 'reset'
  $('#student-settings-cancel').click ->
    $('#student-settings').modal 'hide'

  $('#add-student-i-btn').click ->
    if $name_pattern
      $('#s-i-pattern-username').show()
      $('#s-i-custom-username').hide()
    else
      $('#s-i-custom-username').show()
      $('#s-i-pattern-username').hide()
    if $email_suffix
      $('#s-i-pattern-email').show()
      $('#s-i-custom-email').hide()
    else
      $('#s-i-custom-email').show()
      $('#s-i-pattern-email').hide()
    $('#add-student-i').modal 'show'
  $('#i-s-name').change setup_individual_settings
  $('#cancel-student-i').click ->
    $('#add-student-i').modal 'hide'
  $('#confirm-student-i').click confirm_individual_student

